<template>
	<view class="new">
		<view class="new-header">
			{{$t('tab.message')}}
		</view>
		<ul>
			<li v-for="(item,index) in msgList" :key="index" @click.stop="handleChat(item)">
				<dl>
					<dt>
						<image :src="item.person_headimgurl" mode="aspectFill"></image>
						<span v-if="item.unread>0"></span>
					</dt>
					<dd>
						<view class="new-top">
							<p>{{item.person_name}}</p>
							<span>{{renderMessageDate(item,index)}}</span>
						</view>
						<p><text v-if="item.unread>0">[{{item.unread+'条'}}]</text>{{item.content}}</p>
					</dd>
					</dt>
				</dl>
			</li>
		</ul>
		<view class="empty" v-if="msgList.length<=0">
			<image src="../../static/list.png" mode=""></image>
			<p>暂无消息</p>
		</view>
		<u-loadmore v-if="hasMore&&msgList.length>0" :status="loadStatus" />
	</view>
	</view>
</template>

<script>
	import {
		getChatList,
		getUnreadNum
	} from "@/lib/message";
	import {
		showFailure
	} from '@/lib/helper';
	import {
		mapState
	} from 'vuex';
	import {
		formatDate
	} from '@/utils/chat.js';
	import eventBus from '@/utils/eventBus';
	export default {
		data() {
			return {
				loadStatus: 'loadmore',
				pageNo: 1,
				pageSize: 10,
				hasMore: false,
				msgList: []
			}
		},
		computed: mapState({
			//显示时间
			renderMessageDate() {
				return (message, index) => {
					if (message.timestamp > 3 * 60 * 1000) {
						return formatDate(message.timestamp, 'timestamp');
					}
					return '';
				};
			}
		}),
		onShow() {
			let _this = this;
			_this.pageNo = 1;
			_this.msgList = [];
			_this.loadData();
			_this.getUnreadCount();

		},
		async onLoad() {
			let _this = this;
			eventBus.on('UPDATE_MESSAGE_COLLECTION', (data) => {
				console.log('有人发消息了111/////////////', data)
				_this.pageNo = 1;
				_this.msgList = [];
				_this.loadData();
				_this.getUnreadCount();
			})
		},
		onPullDownRefresh() {
			this.pageNo = 1;
			this.msgList  = [];
			this.loadData(true);
			setTimeout(()=>{
				uni.stopPullDownRefresh();
			},1500)
		},
		onReachBottom() {
			if (this.hasMore) {
				this.pageNo = this.pageNo + 1;
				this.loadData();
			}
		},
		methods: {
			getUnreadCount() {
				getUnreadNum().then((res) => {
					if (res.statusCode == 0) {
						const unreadCount = res.data.number;
						if (unreadCount > 0) {
							uni.setTabBarBadge({
								index: 2, // 消息tab的索引，对应pages.json中的index
								text: unreadCount > 99 ? '...' : String(unreadCount)
							});
						}
					}
				}).catch((res) => {});
			},

			initLoad() {
				this.pageNo = 1;
				this.loadData(true);
			},
			loadData(isReset) {
				this.loadStatus = 'loading';
				let obj = {
					pageNo: this.pageNo,
					pageSize: this.pageSize,
				}
				getChatList(obj).then((res) => {
						uni.hideLoading();
						this.loadStatus = 'no-more';
						this.hasMore = res.data.hasMore;
						if (isReset) {
							this.msgList = res.data.records;
						} else {
							this.msgList = this.msgList.concat(res.data.records);
						}
					})
					.catch((res) => {
						this.loadStatus = 'no-more';
						uni.hideLoading();
						showFailure();
					});
			},
			handleChat(item) {
				uni.navigateTo({
					url: '/pages/new/chat?chatid=' + item.chatid + "&receiverid=" + item.personid+"&name="+item.person_name
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.empty {
		text-align: center;

		image {
			width: 376rpx;
			height: 376rpx;
		}

		p {
			font-size: 28rpx;
			color: #999999;
		}
	}

	.new-header {
		padding: 38rpx 30rpx;
		font-family: Microsoft YaHei UI;
		font-weight: 400;
		font-size: 70rpx;
		color: #000000;
		line-height: 86rpx;
	}

	ul {
		margin: 0 34rpx;
		border-top: 1px solid #F1F1F1;

		li {
			padding: 18rpx 0;
			border-bottom: 1px solid #F1F1F1;

			dl {
				display: flex;
				align-items: cneter;

				dt {
					position: relative;

					image {
						width: 110rpx;
						height: 110rpx;
						border-radius: 12rpx;
						background: #eeeeee;
					}

					span {
						width: 20rpx;
						height: 20rpx;
						background: #FF0000;
						border-radius: 50%;
						border: 2px solid #FFFFFF;
						position: absolute;
						right: -6rpx;
						top: -10rpx;
					}
				}

				dd {
					padding-left: 26rpx;
					flex: 1;

					.new-top {
						display: flex;
						justify-content: space-between;

						p {
							font-family: Microsoft YaHei UI;
							font-weight: 400;
							font-size: 32rpx;
							color: #000000;
							line-height: 38rpx;
						}

						span {
							font-family: Microsoft YaHei UI;
							font-weight: 400;
							font-size: 26rpx;
							color: #999999;
							line-height: 31rpx;
						}
					}

					p {
						font-family: Microsoft YaHei UI;
						font-weight: 400;
						font-size: 24rpx;
						color: #999999;
						margin-top: 18rpx;
					}
				}
			}
		}
	}
</style>